<template>
  <div class="goods-tabs">
    <nav>
      <a href="javascript:;" v-for="item in types" :key="item.id" :class="{active:curName===item.type}" @click="toggle(item.type)">{{item.name}}</a>
    </nav>
    <!-- 这个位置显示对应的组件 GoodsDetail 或者 GoodsComment -->
    <!-- <GoodsDetail v-if="curName==='detail'"/>
    <GoodsComment v-else/> -->
    <component :is="componentId"></component>
  </div>
</template>
<script>
import { ref } from 'vue'
import GoodsDetail from './goods-detail.vue'
import GoodsComment from './goods-comment.vue'
export default {
  name: 'GoodsTabs',
  components: {
    GoodsDetail,
    GoodsComment
  },
  setup () {
    const curName = ref('detail')
    const types = [
      {
        id: 1,
        type: 'detail',
        name: '商品详情'
      },
      {
        id: 2,
        type: 'comment',
        name: '商品评价'
      }
    ]
    // 改变componentId，实现切换
    const componentId = ref('goods-detail')
    function toggle (type) {
      curName.value = type
      componentId.value = `goods-${type}`
    }
    return {
      curName,
      toggle,
      types,
      componentId
    }
  }
}
</script>
<style scoped lang="less">
.goods-tabs {
  min-height: 600px;
  background: #fff;
  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;
    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;
      > span {
        color: @priceColor;
        font-size: 16px;
        margin-left: 10px;
      }
      &:first-child {
        border-right: 1px solid #f5f5f5;
      }
      &.active {
        &::before {
          content: "";
          position: absolute;
          left: 40px;
          bottom: -1px;
          width: 72px;
          height: 2px;
          background: @xtxColor;
        }
      }
    }
  }
}
</style>
